package org.oimwrapper.connectors.genericldap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Reconciles FQDN's of objects specifically from Active Directory. <p>
 *
 * This task expects the following parameters, in addition to the standard parameters in the 
 * {@link OIMLdapLookupReconciliation} class.
 * <ul>
 * <li><code>ExcludeSystemContainers</code> Settings this to <code>true</code> indicates that objects 
 *                                          within certain Active Directory system containers should be 
 *                                          excluded, setting this to <code>false</code> will not exclude
 *                                          objects in the system containers.</li>
 * </ul>
 */
public class OIMLdapADFqdnReconciliation extends OIMLdapLookupReconciliation {

	/**
	 * This method is called once to retrieve a Map of records on the target system.   The key value of the Map
	 * will be used as the encode value, and the value associated with that key will be used as the decode value;
	 * 
	 * @return A Map of records on the target system.
	 * @exception Exception
	 */
	public Map getRecords() throws Exception {
		logger.debug("Entering ::getRecords()");
		Map data = super.getRecords();
		Map result = new HashMap();
		OIMLdapParsingUtils parsingUtils = new OIMLdapParsingUtils(logger);
		for (Iterator iterator = data.keySet().iterator(); iterator.hasNext(); ) {
			String dn = (String) iterator.next();
			String[] pieces = parsingUtils.splitDN(dn);
			String domain = "";
			String path = "";
			for (int i = 0; i < pieces.length; i++) {
				if (pieces[i].substring(0, 3).equalsIgnoreCase("dc=")) {
					if (domain.length() > 0) {
						domain += ".";
					}
					domain += pieces[i].substring(3);
				} else {
					if (path.length() > 0) {
						path = "/" + path;
					}
					path = pieces[i].substring(3) + path;
				}
			}
			logger.debug("domain = " + domain);
			logger.debug("path = " + path);
			if (getBooleanAttribute("ExcludeSystemContainers", false)) {
				if (path.toLowerCase().indexOf("configuration/") == 0 || path.equalsIgnoreCase("configuration")) {
					continue;
				}
				if (path.toLowerCase().indexOf("program data/") == 0 || path.equalsIgnoreCase("program data")) {
					continue;
				}
				if (path.toLowerCase().indexOf("system/") == 0 || path.equalsIgnoreCase("system")) {
					continue;
				}
				if (path.toLowerCase().indexOf("foreignsecurityprincipals/") == 0 || path.equalsIgnoreCase("foreignsecurityprincipals")) {
					continue;
				}
				if (path.toLowerCase().indexOf("microsoft exchange security groups/") == 0 || path.equalsIgnoreCase("microsoft exchange security groups")) {
					continue;
				}
				if (path.toLowerCase().indexOf("microsoft exchange system objects/") == 0 || path.equalsIgnoreCase("microsoft exchange system objects")) {
					continue;
				}
				if (path.toLowerCase().indexOf("microsoftdns/") == 0 || path.equalsIgnoreCase("microsoftdns")) {
					continue;
				}
			}
			logger.debug("Processed " + dn + " to " + domain + "/" + path);
			result.put(dn.toLowerCase(), domain + "/" + path);
		}
		logger.debug("Exiting ::getRecords()");
		return result;
	}
}